﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DrugStorehouseMIS.BLL;
using DrugStorehouseMIS.Comm;

namespace DrugStorehouseMIS.StockInAndOutManager
{
    public partial class frmStockOutHeaderEdit : Form
    {
        private DataTable dtDepartment;
        private StockOutHeader stockOutHeaderBLL;
        private Department departmentBLL;
        private StockOutType stockOutTypeBLL;

        public frmStockOutHeaderEdit()
        {
            InitializeComponent();

            dtDepartment = new DataTable();
            stockOutHeaderBLL = new StockOutHeader();
            stockOutTypeBLL = new StockOutType();
            departmentBLL = new Department();
        }

        /// <summary>
        /// 接收header并设置信息
        /// </summary>
        /// <param name="header"></param>
        public void Init(StockOutHeader header)
        {
            BindStockOutType();
            stockOutHeaderBLL = header;
            SetInfo();
        }

        /// <summary>
        /// 设置信息
        /// </summary>
        public void SetInfo()
        {
            txtDepartment.Text = stockOutHeaderBLL.DepartmentName.DepartmentName;
            txtStockOutNum.Text = stockOutHeaderBLL.StockOutNum;
            dtpStockOutDate.Value = stockOutHeaderBLL.StockOutDatetime;
            cbxStockOutType.SelectedItem = stockOutHeaderBLL.StockOutTypeName.StockOutTypeName;
            rtbRemark.Text = stockOutHeaderBLL.Remark;
        }

        /// <summary>
        /// 绑定出库类型
        /// </summary>
        public void BindStockOutType()
        {
            IList<StockOutType> list = stockOutTypeBLL.GetAllStockOutType();
            foreach (StockOutType obj in list)
            {
                cbxStockOutType.Items.Add(obj.StockOutTypeName);
            }
            cbxStockOutType.SelectedIndex = 0;
        }

        /// <summary>
        /// 根据必填信息是否完整来设置按钮可用状态
        /// </summary>
        /// <returns></returns>
        public bool Check()
        {
            if (txtDepartment.Text.Trim() != "")
                return btnUpdate.Enabled = true;
            else
                return btnUpdate.Enabled = false;
        }

        /// <summary>
        /// 根据已填的信息获取对象
        /// </summary>
        /// <returns></returns>
        public StockOutHeader GetObject()
        {
            StockOutHeader header = new StockOutHeader();
            header.DepartmentName = new Department();
            header.StockOutTypeName = new StockOutType();

            header.StockOutNum = txtStockOutNum.Text.Trim();
            header.StockOutTypeName.StockOutTypeName = cbxStockOutType.Text;
            header.StockOutDatetime = dtpStockOutDate.Value.Date;
            header.DepartmentName.DepartmentName = txtDepartment.Text.Trim();
            header.Remark = rtbRemark.Text;
            header.CreatedBy = frmMain.userObject.UserName;
            header.ApprovalBy = "";
            header.Status = "未审核";

            return header;
        }

        #region 领用单位智能录入

        /// <summary>
        /// 绑定部门信息
        /// </summary>
        public void BindDepartment()
        {
            dgvDepartment.DataSource = dtDepartment = departmentBLL.Display();
        }

        private void txtDepartment_TextChanged(object sender, EventArgs e)
        {
            int times = 0;
            this.Check();

            this.dgvDepartment.Top = this.txtDepartment.Top + this.txtDepartment.Height;

            if (times == 0)
            {
                times = 1;
                BindDepartment();
            }

            if (txtDepartment.Text == "")
            {
                if (dtDepartment.DefaultView.Count != 0)
                    dgvDepartment.Visible = true;
            }

            dtDepartment.DefaultView.RowFilter = "department_code like '" + txtDepartment.Text.Trim() + "%'";

            if (dtDepartment.DefaultView.Count != 0)
            {
                dgvDepartment.Visible = true;
                dgvDepartment.CurrentCell = dgvDepartment[0, 0];
            }
            else
                dgvDepartment.Visible = false;
        }

        private void dgvDepartment_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                if (dgvDepartment.Rows.Count == 0)
                    return;

                dgvDepartment.Focus();
                if (dgvDepartment.CurrentRow.Index + 1 < dgvDepartment.Rows.Count)
                {
                    dgvDepartment.CurrentCell = dgvDepartment[0, dgvDepartment.CurrentRow.Index + 1];
                }
            }
            if (e.KeyCode == Keys.Enter)
            {
                txtDepartment.Text = dgvDepartment.CurrentRow.Cells[1].Value.ToString();
                dgvDepartment.Visible = false;
            }
        }

        private void txtDepartment_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                if (dgvDepartment.Rows.Count == 0)
                    return;

                dgvDepartment.Focus();
                if (dgvDepartment.CurrentRow.Index + 1 < dgvDepartment.Rows.Count)
                {
                    dgvDepartment.CurrentCell = dgvDepartment[0, dgvDepartment.CurrentRow.Index + 1];
                }
            }

            if (e.KeyCode == Keys.Enter)
            {
                if (dgvDepartment.Visible == true)
                {
                    txtDepartment.Text = dgvDepartment.CurrentRow.Cells[1].Value.ToString();
                    dgvDepartment.Visible = false;
                }
            }
        }

        #endregion

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            stockOutHeaderBLL.SetStockOutHeader(GetObject());
            this.DialogResult = DialogResult.OK;
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtDepartment.Text = "";
            dtpStockOutDate.Value = DateTime.Now;
            rtbRemark.Text = "";
            cbxStockOutType.SelectedIndex = 0;
            dgvDepartment.Visible = false;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnRecover_Click(object sender, EventArgs e)
        {
            SetInfo();
        }
    }
}